package com.nzw.schedule.job;


import com.nzw.service.member.MemberService;
import com.nzw.service.schedule.quartz.ScheduleService;
import com.nzw.service.shop.ShopService;
import com.nzw.utils.DateTimeUtilT;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * 自定义定时器
 *
 * create BY yinwx on  2018/05/02
 *
 */

public class MyQuartzJobBean extends QuartzJobBean{



    @Autowired
    ShopService shopService;
    @Autowired
    MemberService memberService;
    @Autowired
    ScheduleService scheduleService;



    public static final String CHACK_CARD_ORDER = "cardchackOrder";//检查刷卡支付中订单是否支付
    public static final String SET_MEMBERGROUP = "setmemberGroup";  //会员组自动设置
    public static final String SEND_BIRTHCOUPON = "sendBirthCoupon";  //发送生日券
    public static final String TIMEER_TEST = "timertest";  //发送生日券



    public Logger logger = Logger.getLogger(QuartzJobBean.class);


    @Override
    protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
        org.quartz.Trigger trigger = jobexecutioncontext.getTrigger();
        String triggerName = trigger.getKey().getName();
        String groupName = trigger.getKey().getGroup();
        logger.info("正在执行任务【" + triggerName + "】，group【" + groupName + "】"+" time:"+ DateTimeUtilT.dateTime(trigger.getStartTime())+",当前线程："+Thread.currentThread().getName());
        switch (groupName) {

            case CHACK_CARD_ORDER:

                System.out.println("你开启了支付查询定时器"+triggerName);
                String [] arrstr = triggerName.split("_");
                String orderCode = arrstr[0];
                System.out.println(arrstr[1]);
                int i = Integer.valueOf(arrstr[1]);

                if(i >= 10){
                    System.out.println("支付时间超过20s取消订单");

                    //payService.cancelPayOrder(shop.getMacid(),orderCode);
                    return;
                }

                break;
            case SET_MEMBERGROUP:
                memberService.setMmeberGroup();
                System.out.println("会员组设置完毕");
                break;
            case SEND_BIRTHCOUPON:

                //couponService.sendBorthCoupon();
                System.out.println("发送生日券完毕");
                break;
            case TIMEER_TEST:
                System.out.println("timerTest=============");
                Date date = new Date();
                scheduleService.scheduleEvenIfDateAfterNow("setMemberGroup", new Date(date.getTime()+10000), MyQuartzJobBean.TIMEER_TEST);
                break;

        }
    }

    private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
        try {
            return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
        } catch (SchedulerException e) {
            System.out.println("jobexecutioncontext.getScheduler().getContext() error!");
            throw new RuntimeException(e);
        }
    }
}
